<!--  -->
<template>
  <div>
    <van-nav-bar fixed>
  <template #left>
    <img :src="imgObj" alt="" class="logo">
  </template>
  <template #right>
    <van-icon name="search" color="#fff" size="0.48rem" @click="$router.push('/search')"/>
  </template>
</van-nav-bar>

<van-tabs
  v-model="channelId"
      animated
      sticky
      offset-top="1.226667rem"
>

  <van-tab v-for="item in channelList" :key="item.id" :title="item.name" :name="item.id">
        <ArticleList :cid="channelId"></ArticleList>
  </van-tab>
</van-tabs>
<van-icon name="plus" size="0.37333334rem" class="moreChannels" @click="show = true"/>
    <!-- 弹出层组件 -->
    <van-popup v-model="show" class="edit_wrap" @closed="onPopupClosed">
      <!-- 弹出层的主体区域 -->
      <channel-edit :userChannelList= "channelList"
      @close="show=false" @addChannel="addChannelFn"
      @removeChannel="removeChannelFn" ref="channelEdit" @changeChannel="channgeChannelFn">

      </channel-edit>
    </van-popup>
    </div>
</template>

<script>
import LogoPng from '../../assets/toutiao_logo.png'
import { userChannelAPI, updateChannelAPI } from '@/api'
import ArticleList from '@/views/Home/ArticleList'
import ChannelEdit from './ChannelEdit.vue'

export default {
  data () {
    return {
      imgObj: LogoPng,
      channelId: 0,
      channelList: [],
      show: false
    }
  },
  async created () {
    const res = await userChannelAPI()
    this.channelList = res.data.data.channels
  },
  components: {
    ArticleList, ChannelEdit
  },
  methods: {
    async addChannelFn (val) {
      this.channelList.push(val)
      this.updateChannel()

      const list = this.channelList.filter(item => {
        return item.id !== 0
      })
      const resArr = list.map((item, index) => {
        const newObj = { ...item }
        newObj.seq = index + 1
        delete newObj.name
        return newObj
      })
      await updateChannelAPI({
        channels: resArr
      })
    },
    removeChannelFn (val) {
      const index = this.channelList.findIndex(item => item.id === val.id)
      this.channelList.splice(index, 1)
      this.updateChannel()
    },
    async updateChannel () {
      const list = this.channelList.filter(item => {
        return item.id !== 0
      })
      const resArr = list.map((item, index) => {
        const newObj = { ...item }
        newObj.seq = index + 1
        delete newObj.name
        return newObj
      })
      console.log(resArr)
      await updateChannelAPI({
        channels: resArr
      })
    },
    onPopupClosed () {
      this.$refs.channelEdit.isEdit = false
    },
    channgeChannelFn (val) {
      this.channelId = val.id
    }
  }

}

</script>
<style lang='less' scoped>
.logo{
  width: 100px;
  height: 30px;
}
/deep/ .van-tabs__content{
    padding-top: 44px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}
.edit_wrap{
  width: 100%;
  height: 100%;
}
</style>
